include('shared.lua')
require('datastream')

function msggunrecycler(msg)
	local gunlist = msg:ReadShort()
	local vault = msg:ReadShort()
	local processing = msg:ReadBool() 
	local time = msg:ReadShort()
	local inputenabled = false
	if(HelpToggled or GUIToggled) then
		inputenabled = true
	end
	local panel = vgui.Create("Frame")
	panel:SetPos(ScrW()/2-200 , ScrH() / 2 - 50)
	panel:SetName("Panel")
	panel:LoadControlsFromString([[
	
		"GVPanel"
		{
		
			"Panel"
			{
			
				"ControlName" "Panel"
				"fieldName" "Vote"
				"wide" "160"
				"tall" "110"
				"sizable" "0"
				"enabled" "1"
				"title" "Gun Recycler"
			
			}
		
		}
	
	]])
	panel:SetKeyboardInputEnabled(false)
	panel:SetMouseInputEnabled(true)
	panel:SetVisible(true)
	local divider = vgui.Create("Divider")
	divider:SetParent(panel)
	divider:SetPos(2, 30)
	divider:SetSize(180, 2)
	divider:SetVisible(true)
	if(processing == false) then
		local label = vgui.Create("Label")
		label:SetParent(panel)
		label:SetPos(15, 45)
		label:SetSize(150, 15)
		label:SetText(time .. " Seonds left processing.")
		label:SetVisible(true)
		table.insert(GVGUI, label)
		
		label = vgui.Create("Button")
		label:SetParent(panel)
		label:SetPos(15, 65)
		label:SetSize(130, 15)
		label:SetCommand("!")
		label:SetText("Cancel Gun Recycling")
		label:SetActionFunction(function() LocalPlayer():ConCommand("processgun " .. vault .. " " .. 0 .. "\n") end)
		label:SetVisible(true)
		table.insert(GVGUI, label)
	elseif(gunlist ~= 0) then
		local label = vgui.Create("Label")
		label:SetParent(panel)
		label:SetPos(15, 40)
		label:SetSize(130, 25)
		label:SetText(gunlist .. " Guns inserted.")
		label:SetVisible(true)
		table.insert(GVGUI, label)
		
		label = vgui.Create("Button")
		label:SetParent(panel)
		label:SetPos(15, 65)
		label:SetSize(130, 15)
		label:SetCommand("!")
		label:SetText("Process Guns")
		label:SetActionFunction(function() LocalPlayer():ConCommand("processgun " .. vault .. " " .. 1 .. "\n") end)
		label:SetVisible(true)
		table.insert(GVGUI, label)
		
		label = vgui.Create("Button")
		label:SetParent(panel)
		label:SetPos(15, 80)
		label:SetSize(130, 15)
		label:SetCommand("!")
		label:SetText("Eject Guns")
		label:SetActionFunction(function() LocalPlayer():ConCommand("processgun " .. vault .. " " .. 2 .. "\n") end)
		label:SetVisible(true)
		table.insert(GVGUI, label)
	else
		local label = vgui.Create("Label")
		label:SetParent(panel)
		label:SetPos(15, 45)
		label:SetSize(130, 40)
		label:SetText("This Gun Recycler is empty.")
		label:SetVisible(true)
		table.insert(GVGUI, label)
	end
	PanelNumg = PanelNumg + 1
	GVGUI[vault] = panel
end
usermessage.Hook("msggunrecycler", msggunrecycler)

function killgunrecyclergui(msg)

	local vault = msg:ReadShort()

	if(GVGUI[vault]) then
	
		for k, v in pairs(GVGUI) do
		
			if(v:GetParent() == GVGUI[vault]) then
			
				v:Remove()
				GVGUI[k] = nil
			
			end
		
		end

		GVGUI[vault]:Remove()
		
		GVGUI[vault] = nil
		
		PanelNumg = PanelNumg - 1
		
	end

end
usermessage.Hook("killgunrecyclergui", killgunrecyclergui)